var customer = new i18nMessage('../../servlet/BiFranceCustomerServlet?action=5');
var ctMaterialType = new i18nMessage('../../servlet/CodeTableServlet?name=CtMaterialType');
var ctCollarType = new i18nMessage('../../servlet/CodeTableServlet?name=CtCollarType');
var ctYesno = new i18nMessage('../../servlet/CodeTableServlet?name=CtYesno');
var ctBrand = new i18nMessage('../../servlet/CodeTableServlet?name=CtBrand');
var ctMaille = new i18nMessage('../../servlet/CodeTableServlet?name=CtMaille');
var ctZipType = new i18nMessage('../../servlet/CodeTableServlet?name=CtZipType');
var ctSupplyType = new i18nMessage('../../servlet/CodeTableServlet?name=CtSupplyType');
var zipStore;
var zipGrid;
var zipWindow;
var selectZipCallback;
var selectedZip;
function locateZip(rowIndex){
	selectedZip = zipStore.getAt(rowIndex).data;
}
var selectZip = function(selectCallback){
	selectZipCallback = selectCallback;
    if (!zipWindow) {
        zipWindow = new Ext.Window({
            title: message.get('message.combo.pleaseselect'),
            layout: 'fit',
            width: 900,
            height: 605,
            closeAction: 'hide',
            plain: true,
            modal: true,
            items: [{
                id: 'zipPanel',
                region: 'center',
                bodyStyle: {
                    background: '#ffffff',
                    padding: '7px'
                },
                html: '<div id="zipDiv"></div>'
            }]
        });
    }
    zipWindow.show();
    
    if (zipGrid) {
		zipStore.reload();
		return;
	}
    zipStore = new Ext.data.Store({
         url: '../../servlet/BiAccessoryZipServlet?action=4',
        reader: new Ext.data.XmlReader({
            record: 'item',
            id: 'id',
            totalRecords: 'itemTotal'
        }, [
        'index', 'reference', 'zipLength', 'ctMaille', 'ctZipType', 'ctSupplyType','ctSupplytypeCode', 'comment', 'photoName',{
		 	name: 'biFranceCustomer.name',
		 	mapping: 'customerName'
		 }, {
		 	name: 'biFranceCustomer.department',
		 	mapping: 'customerDepartment'
		 }, {
		 	name: 'biFactory.nameCN',
		 	mapping: 'factoryNameCN'
		 }, {
		 	name: 'biFactory.nameEN',
		 	mapping: 'factoryNameEN'
		 },'customerReference','customerColor', 'factoryReference', 'factoryColor', 'addressFactory','ctWeaveType', 'ctZipSize', 'ctSlider', 'ctPuller', 'id']),
        
        remoteSort: true
    });
    
    zipStore.setDefaultSort('id', 'desc');
    
    var zipFilters =  new Ext.grid.GridFilters({
		  filters:[
		    {type: 'string',  dataIndex: 'name'},{
			      type: 'string',  
			      dataIndex: 'reference'
		    },{
			      type: 'string',  
			      dataIndex: 'zipLength'
		    },{
			      type: 'list',  
			      dataIndex: 'ctMaille', 
			      options: ctMaille.getCtArray()
		    },{
			      type: 'list',  
			      dataIndex: 'ctZipType', 
			      options: ctZipType.getCtArray()
		    },{
			      type: 'list',  
			      dataIndex: 'ctSupplyType', 
			      options: ctSupplyType.getCtArray()
		    },
		    {type: 'string', dataIndex: 'biFranceCustomer.nameCN'},
		    {type: 'string', dataIndex: 'biFranceCustomer.nameEN'},
			{type: 'string', dataIndex: 'biFranceCustomer.department'},
			{type: 'string', dataIndex: 'biFactory.name'}
	]});
    
    function renderSelectZip(value, p, record, rowIndex){
        return String.format('<a href="javascript: locateZip({13}); selectZipCallback(\'{0}\', \'{1}\',\'{2}\', \'{3}\',\'{4}\', \'{5}\',\'{6}\', \'{7}\',\'{8}\', \'{9}\',\'{10}\', \'{11}\',\'{12}\');zipWindow.hide();"><img src="../../images/icons/arrow_rotate_clockwise.png" title="' + message.get('message.list.select') + '" /></a>', 
        record.data.id, 
        record.data.reference, 
        record.data['biFranceCustomer.name'],
        record.data['biFranceCustomer.department'], 
        record.data.customerReference, 
        record.data.customerColor,
        record.data['biFactory.nameCN'],
        record.data.factoryReference, 
        record.data.factoryColor, 
        record.data.addressFactory, 
        record.data.photoName,
        record.data.isSpecialCode,
        record.data['biFactory.nameEN'],
        rowIndex);
    }
    
    var zipExpander = new Ext.grid.RowExpander({
         tpl : new Ext.XTemplate(
			        	'<p><b>' + azMessage.get('ui.accessoryzip.zipweave') + ':</b>&nbsp;&nbsp;{ctWeaveType}&nbsp;&nbsp;&nbsp;&nbsp;<b>' + azMessage.get('ui.accessoryzip.zipsize') + ':</b>&nbsp;&nbsp;{ctZipSize}&nbsp;&nbsp;&nbsp;&nbsp;<b>' + message.get('message.edit.materialtype') + ':</b>&nbsp;&nbsp;{ctMaille}&nbsp;&nbsp;&nbsp;&nbsp;<b>' + azMessage.get('ui.accessoryzip.slider') + ':</b>&nbsp;&nbsp;{ctSlider}</p><br/> ',
			        	'<br/><p><img src="../../_temp/{photoName}" width="150px"/></p>', 
						'<br/><p><b>' + message.get('message.edit.comment') + ':</b> {comment}</p><br/>'
			        )
    });
    
    // create the grid
    zipGrid = new Ext.grid.GridPanel({
        title: azMessage.get('ui.accessoryzip.listtitle'),
        store: zipStore,
        stripeRows: true,
        plugins: [zipFilters, zipExpander],
        height: 560,
        columns: [zipExpander, {
            header: message.get('message.list.id'),
            width: 50,
            dataIndex: 'index',
            sortable: false
        }, {
            header: azMessage.get('ui.accessoryzip.reference'),
            dataIndex: 'reference',
            sortable: true
        },{
            header: message.get('message.edit.buyer'),
            dataIndex: 'ctSupplyType',
            sortable: true
        },{
            header: fcMessage.get('ui.francecustomer.name'),
            dataIndex: 'biFranceCustomer.name',
            sortable: true
        },{
            header: fcMessage.get('ui.francecustomer.department'),
            dataIndex: 'biFranceCustomer.department',
            sortable: true
        },{
            header: azMessage.get('ui.accessoryzip.customerreference'),
            dataIndex: 'customerReference',
            sortable: true
        },{
            header: azMessage.get('ui.accessoryzip.customercolor'),
            dataIndex: 'customerColor',
            sortable: true
        },{
            header: fMessage.get('ui.factory.name') + '(CN)',
            dataIndex: 'biFactory.nameCN',
            sortable: true
        },{
            header: fMessage.get('ui.factory.name') + '(EN)',
            dataIndex: 'biFactory.nameEN',
            sortable: true
        },{
            header: azMessage.get('ui.accessoryzip.factoryreference'),
            dataIndex: 'factoryReference',
            sortable: true
        },{
            header: azMessage.get('ui.accessoryzip.factorycolor'),
            dataIndex: 'factoryColor',
            sortable: true
        },{
            header: message.get('message.edit.materialtype'),
            dataIndex: 'ctMaille',
            sortable: true
        },{
            header: azMessage.get('ui.accessoryzip.puller'),
            dataIndex: 'ctPuller',
            sortable: true
        },{
            header: azMessage.get('ui.accessoryzip.length') + '(cm)',
            dataIndex: 'zipLength',
            sortable: true
        }, {
            header: message.get('message.list.select'),
            width: 30,
            dataIndex: 'id',
            sortable: false,
            renderer: renderSelectZip
        }],
        viewConfig: {
            forceFit: true
        },
        loadMask: true,
        renderTo: 'zipDiv',
        bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: zipStore,
            displayInfo: true
        })
    });
    
    var startPage = getPrarmeterValue(window.location, 'startPage');
    var start;
    if (startPage == null || startPage == '') {
        start = 0;
    }
    else {
        start = (startPage - 1) * 20;
    }
    zipStore.load({
        params: {
            start: start,
            limit: 20
        }
    });
}

